package org.daochong.fastdev.funcs;

import java.sql.SQLException;

import org.daochong.fastdev.Context;
import org.daochong.fastdev.Function;
import org.daochong.fastdev.util.FastDevUtils;
import org.daochong.lang.EventHandler;
import org.daochong.lang.StringUtils;

public class EventFunction implements Function {

	public boolean is(String id) {
		return FastDevUtils.operNumber("event", id);
	}

	public void exec(String key, String prop, Context context) throws SQLException {
		if (StringUtils.isEmpty(prop) || !is(key))
			return;
		String[] ar = prop.split(":");
		EventHandler handler = context.getBean(EventHandler.class);
		if (handler == null) {
			throw new RuntimeException("Event Hanlder is null");
		}
		Object o = context.searchValue(prop);
		if (o == null) {
			throw new RuntimeException("Event Hanlder Object " + prop + " is null");
		}
		if (ar.length == 1) {
			handler.notify(o);
		} else {
			handler.notify(o, ar[1].trim());
		}
	}

}
